package cn.iocoder.yudao.module.member.dal.mysql.cardsecurity;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.point.vo.recrod.MemberPointRecordPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.cardsecurity.vo.CardSecurityQueryVo;
import cn.iocoder.yudao.module.member.dal.dataobject.cardsecurity.CardSecurityDo;
import cn.iocoder.yudao.module.member.dal.dataobject.point.MemberPointRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface CardSecurityMapper extends BaseMapperX<CardSecurityDo> {
   default Long selectCountByUserId(Long userId){
       return selectCount(new LambdaQueryWrapperX<CardSecurityDo>()
               .eq(CardSecurityDo::getUserId, userId)
               .eq(CardSecurityDo::getUseFlag, Boolean.FALSE));
   }

    default CardSecurityDo selectCountByCardSecurityCode(String cardSecurityCode){
       return selectOne(new QueryWrapper<CardSecurityDo>().eq("card_security", cardSecurityCode));
    }

    default PageResult<CardSecurityDo> selectPage(CardSecurityQueryVo reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CardSecurityDo>()
                .eqIfPresent(CardSecurityDo::getUserId, reqVO.getUserId())
                .likeIfPresent( CardSecurityDo::getCardSecurity, reqVO.getCardSecurity())
                .orderByDesc(CardSecurityDo::getId));
    }

    default  void updateStatusByIds(List<Long> ids , Integer status){
        LambdaUpdateWrapper<CardSecurityDo> lambdaUpdateWrapper = new LambdaUpdateWrapper<CardSecurityDo>()
                .set(CardSecurityDo::getStatus, status)
                .in(CardSecurityDo::getId, ids);
        update(null, lambdaUpdateWrapper);
    }


    default CardSecurityDo selectLastOneByUserId(Long userId){
        List<CardSecurityDo> cardSecurityDos = selectList(new LambdaQueryWrapper<CardSecurityDo>().eq(CardSecurityDo::getUserId, userId).orderByDesc(CardSecurityDo::getId));
        if (cardSecurityDos.isEmpty()){
            return null;
        }
        return cardSecurityDos.get(0);
    }

    default List<CardSecurityDo> selectCardList(String operateCode){
       return selectList(new LambdaQueryWrapper<CardSecurityDo>().eq(CardSecurityDo::getOperateCode, operateCode));
    }
}
